/**
 * JournalServiceImpl.java
 * @author
 * 作者 屈甲康 
 */
package org.jackysoft.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.Journal;
import org.jackysoft.entity.User;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.JournalService;
import org.jackysoft.util.IOUtils;
import org.jackysoft.util.SpringSecurityUtils;
import org.joda.time.DateTime;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class JournalServiceImpl extends AbstractStringIdServiceImpl<Journal>
implements JournalService {
    /**
	 * 
	 */
	private static final long serialVersionUID = -277317076208161122L;
	final static Log logger = LogFactory.getLog(JournalServiceImpl.class);

	public JournalServiceImpl(){
	    this.type = Journal.class;
	}	
		


	/* (non-Javadoc)
	 * @see org.jackysoft.service.AbstractStringIdServiceImpl#update(org.jackysoft.entity.Identifiable)
	 */
	@Override
	public void update(Journal b) throws DataAccessException {
		
		byte[] content = null;
		//TODO 用pageoffice 获取字节内容
		Journal nb = get(b.getId());
		nb.setData(content);		
	}




	protected byte[] loadEmptyTemplate(String template) {
		if(template==null)throw new IllegalStateException("Excle 模版名称不能为空");
		InputStream ins = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			org.springframework.core.io.Resource res = resourceLoader
					.getResource(String.format("classpath:%s.xls",template));
			ins = res.getInputStream();
			IOUtils.copy(ins, bos);
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				ins.close();
			} catch (IOException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
		return bos.toByteArray();
	}




	@Override
	public Journal create(DateTime date) {
		if(date==null)date = DateTime.now();
		User owner = SpringSecurityUtils.getCurrentUser();
		Department department = owner.getDepartment();
		String hql=String.format(" from %s j where j.department=:dept " 
		        //+
				//" and year(j.createDate)=:year" +
				//" and month(j.createDate)=:month " +
				//" and day(j.createDate)=:day " 
				,
				entitytable());
		Iterator itr = query(hql)
				.setParameter("dept", department)
				//.setParameter("year", date.getYear())
				//.setParameter("month", date.getMonthOfYear())
				//.setParameter("day", date.getDayOfMonth())
				.iterate();
		if(itr.hasNext())return (Journal) itr.next();
		byte[] content = loadEmptyTemplate("journal");
		Journal nb =  new Journal();
		nb.setData(content);
		nb.setCreateDate(date);	  
		nb.setDepartment(department);
	    nb.setLastuser(owner);
	    this.create(nb);
		return nb;
	}
	
}